﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Calendar;
using TimeSnapperPluginAPI;

namespace GCalPlugin
{
    public partial class GCalConfigurationForm : Form
    {

        private IServices m_services;
        public const string gcal_PluginEnabled = "GCalPlugin.Enabled";
        public const string gcal_User = "GCalPlugin.User";
        public const string gcal_Pass = "GCalPlugin.Pass";
        public const string gcal_Feed = "GCalPlugin.Feed";
        private Dictionary<string, Uri> feeds;

        public GCalConfigurationForm(IServices services)
        {
            InitializeComponent();
            m_services = services;


            isEnabled.Checked = m_services.SettingsServices.GetPropertyValue(gcal_PluginEnabled, "true")=="true";
            Username.Text = m_services.SettingsServices.GetPropertyValue(gcal_User, string.Empty);
            Password.Text = m_services.SettingsServices.GetPropertyValue(gcal_Pass, string.Empty);

            if (string.IsNullOrEmpty(Username.Text) || string.IsNullOrEmpty(Password.Text))
            {
                noFeeds();
                return;
            }
            //try to load feeds
            if (!FillComboFeeds())
            {
                return;
            }
            var feedUri = m_services.SettingsServices.GetPropertyValue(gcal_Feed, string.Empty);
            if(feeds.All(f => f.Value.ToString() != feedUri))
            {
                btnOk.Enabled = false;
                return;
            }
            Feed.SelectedItem = feeds.Single(f => f.Value.ToString() == feedUri).Key;
            Btn_TestFeed.Enabled = false;
        }

        private bool FillComboFeeds()
        {
            Feed.Items.Clear();
            var journal = new GCalJournal(Username.Text, Password.Text,null);
            feeds = journal.LoadFeeds();
            if (feeds == null)
            {
                noFeeds();
                return false;
            }
            foreach (var pair in feeds)
            {
                Feed.Items.Add(pair.Key);
            }
            gb_Calendar.Enabled = true;
            Btn_TestCredentials.Enabled = false;
            return true;
        }

        private void noFeeds()
        {
            gb_Calendar.Enabled = false;
            Btn_TestCredentials.Enabled = true;
            btnOk.Enabled = false;
        }



        private void btnOk_Click(object sender, EventArgs e)
        {
            // persist the settings to the database
            m_services.SettingsServices.SetPropertyValue(gcal_PluginEnabled, isEnabled.Checked?"true":"false");
            m_services.SettingsServices.SetPropertyValue(gcal_User, Username.Text);
            m_services.SettingsServices.SetPropertyValue(gcal_Pass, Password.Text);
            m_services.SettingsServices.SetPropertyValue(gcal_Feed, feeds[(string)Feed.SelectedItem].ToString());
        }

        private void Btn_TestCredentials_Click(object sender, EventArgs e)
        {
            FillComboFeeds();
        }

        private void Btn_TestFeed_Click(object sender, EventArgs e)
        {
            //TODO try to insert a dummy even to check we can write
            //for now just assume it is working
            btnOk.Enabled = true;
            Btn_TestFeed.Enabled = false;
        }

        private void Feed_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnOk.Enabled = false;
            Btn_TestFeed.Enabled = true;
        }

        private void Credentials_Changed(object sender, EventArgs e)
        {
            noFeeds();
            Btn_TestCredentials.Enabled = true;
        }

        private void isEnabled_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBox1.Enabled = isEnabled.Checked;
            if(isEnabled.Checked == false)
            {
                btnOk.Enabled = true;
            }
            else
            {
                Credentials_Changed(sender,e);
            }
        }
    }
}
